-- 运营活动: 许愿井


--[[
{
    cmd_id = 1, -- 命令id，用于批量停止、删除活动 
    act_id = 77, -- 活动id (77 许愿井)
    act_name = "许愿井", -- 活动名称
    act_desc = "活动期间获取经验达到对应档次则可领取丰厚奖励！", -- 活动描述
    beg_time = 1493262000,  -- 活动开始时间(unix timestamp)
    end_time = 1593267400,  -- 活动结束时间(unix timestamp)
    config = -- 配置数据
    {
        everyCountPerSecond = 1800, --多少秒增加一次 
        maxCount = 10,  --最大次数
        saveCount = 50, --保存最多信息
        itemList =  
        {   
            { weight = 1, type = 0, id = 508, count = 2,},
            { weight = 1, type = 0, id = 571, count =30,},
            { weight = 1, type = 0, id = 563, count = 3,},
            { weight = 1, type = 0, id = 558, count = 3,},
            { weight = 1, type = 0, id = 568, count = 3,},
            { weight = 1, type = 0, id = 524, count = 2,},
            { weight = 1, type = 0, id = 529, count = 2,},
            { weight = 1, type = 0, id = 534, count = 2,},
            { weight = 1, type = 0, id = 539, count = 2,},
            { weight = 1, type = 0, id = 763, count =10,},
            { weight = 1, type = 0, id = 509, count = 1,},
        },  
    },
}
]]

-- 检查配置是否正确，正确则返回 0，否则返回错误码
function oper_act_77_check_cfg(cfg)
    if cfg == nil then return 20 end
    
    local config = cfg.config
    if config == nil then return 21 end
    
    if config.Awards == nil then return 23 end
    if type(config.Awards) ~= "table" then return 24 end

    for idx = 1, #config.Awards do
        local preAwards = config.Awards[idx]
        if preAwards == nil then return 25 end
        if type(preAwards) ~= "table" then return 26 end
        if preAwards.awards == nil then return 27 end
        if type(preAwards.awards) ~= "table" then return 28 end
        if not oper_act_check_one_award_cfg(preAwards.awards) then
            return 22
        end 
    end

    return 0
end

-- 安装活动，成功则返回 0，否则返回错误码
function oper_act_77_setup(cfg) 
    return 0
end

-- 卸载活动
function oper_act_77_unsetup(cfg)   
end

-- 活动开始
function oper_act_77_start(cfg)
    local ssVar = System.getStaticVar()
    ssVar.oper_act_77_start = 1
    ssVar.oper_act_77_cmd = cfg.cmd_id
    ssVar.oper_act_77_act = cfg.act_id

    local data = gRankMgr_GetValueByIndx(0, RANK_DEFINE_OPERATION_77, 1)

    local needClearData = true

    if data then
        local myData = BaseFuc_unserialize(data)
        if myData and myData.cmd_id and myData.cmd_id == cfg.cmd_id and myData.act_id and myData.act_id == cfg.act_id and
            myData.time and cfg.beg_time <= myData.time and  cfg.end_time > myData.time then
            needClearData = false
        end
    end

    if needClearData then
        gRankMgr_Clear(RANK_DEFINE_OPERATION_77)
    end

    local players = LuaHelp.getAllActorList()
    if players then 
        for k, v in ipairs(players) do  
            oper_act_77_login(v)
        end
    end

end

-- 活动结束
function oper_act_77_end(cfg)
    local ssVar = System.getStaticVar()
    ssVar.oper_act_77_start = nil
    ssVar.oper_act_77_cmd = nil
    ssVar.oper_act_77_act = nil

    if not cfg then return end
    local config = cfg.config
    if not config then return end
end

-- 打包数据
function oper_act_77_packet_data(sysarg, cfg)
    if not cfg then return end
    local config = cfg.config
    if not config then return end

end

-- 操作，返回操作结果，0为成功
function oper_act_77_do(sysarg, cfg, clientPkt)
    if cfg == nil then return end
    
    if cfg.state == nil or cfg.state ~= 1 then -- 不在活动中
        Actor.sendTipmsg(sysarg,Lang.ScriptTips.Operation001, ttFlyTip)
        return
    end
    
    local config = cfg.config
    if config == nil then -- 参数错误
        print("error, oper_act_77_do cfg.config confg error")
        return
    end 
end


-- 跨天
function oper_act_77_new_day_arrive(act_id) 
    local ssVar = System.getStaticVar()
    if not ssVar.oper_act_77_start then
        return
    end
    if not ssVar.oper_act_77_cmd then
        return
    end
    if not ssVar.oper_act_77_act then
        return
    end
    local cfg = get_oper_act_cfg(ssVar.oper_act_77_cmd, ssVar.oper_act_77_act)
    if not cfg then
        return
    end
    local config = cfg.config
    if not config then return end

    local players = LuaHelp.getAllActorList()
    if players then 
        for k, v in ipairs(players) do  
            oper_act_77_login(v)
        end
    end
end

--登入
function oper_act_77_login(sysarg)
    local ssVar = System.getStaticVar()
    if not ssVar.oper_act_77_start then
        return
    end
    if not ssVar.oper_act_77_cmd then
        return
    end
    if not ssVar.oper_act_77_act then
        return
    end
    local cfg = get_oper_act_cfg(ssVar.oper_act_77_cmd, ssVar.oper_act_77_act)
    if not cfg then
        return
    end
    local config = cfg.config
    if not config then return end


    local actorid = Actor.getActorId(sysarg)
    local data = gRankMgr_GetValueById(actorid, RANK_DEFINE_OPERATION_77, 1)

    local myData = BaseFuc_unserialize(data)
    

    if not myData then
        myData = {}
    end

    if not myData.myGoldData then
        myData.myGoldData = {}
    end
    
    local time = System.getSystemTime()

    for idx = 1, #config.Awards do
        local myConfig = config.Awards[idx]
        if myConfig.star_time <= myConfig.end_time and time >= myConfig.star_time and time <= myConfig.end_time and
            not myData.myGoldData[idx] then
            myData.myGoldData[idx] = 1
            local rewards = myConfig.awards
            local actorid = Actor.getActorId(sysarg)
            local title = string.format(Lang.ScriptTips.Operation207, cfg.act_name)
            local content = string.format(Lang.ScriptTips.Operation208, cfg.act_name, myConfig.desc)
            local sex = Actor.getIntProperty(sysarg, PROP_ACTOR_SEX)
            local vocation = Actor.getIntProperty(sysarg, PROP_ACTOR_VOCATION)
            Awards.GiveToMailById(actorid, rewards, title, content, LogIds.OperationLogId077, Lang.LogTips.Operation077, vocation, sex)
        end
    end

    if not myData.cmd_id then
        myData.cmd_id = cfg.cmd_id
        myData.act_id = cfg.act_id
        myData.time = time
    end

    myData.mySelfTime = time

    local newData = BaseFuc_serialize(myData)
    gRankMgr_SetRankEx(actorid, RANK_DEFINE_OPERATION_77, 1, newData)
end